package com.dcits.electronic_account.service.impl;

import com.dcits.electronic_account.bean.BankAccount;
import com.dcits.electronic_account.dto.BindingBankDTO;
import com.dcits.electronic_account.dto.CancelBindingDTO;
import com.dcits.electronic_account.mapper.BankAccountMapper;
import com.dcits.electronic_account.mapper.VirtualBankMapper;
import com.dcits.electronic_account.service.VirtualBankService;
import com.dcits.electronic_account.vo.BindingRelationsVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 魏渝辉
 * @Date:2022年06月25日 18:45
 * @Description:
 */
@Service
public class VirtualBankServiceImpl implements VirtualBankService {

    @Autowired
    private BankAccountMapper bankAccountMapper;

    @Autowired
    private VirtualBankMapper virtualBankMapper;

    @Override
    public Map<String,String> bindingBankCard(BindingBankDTO bindingBankDTO) {
        HashMap<String,String> result = new HashMap();
        BankAccount bankAccount = bankAccountMapper.selectBankAccountByBindingBankDTO(bindingBankDTO);
        //验证卡号信息是否属于本人
        if(null != bankAccount){//验证成功
            //校验密码
            if (bankAccount.getPassword().equals(bindingBankDTO.getPayPassword())){
                //进行绑卡操作
                virtualBankMapper.insertVirtualBank(bindingBankDTO.getBankCardNumber(),bindingBankDTO.getId());
                result.put("success","银行卡绑定成功");
            }else{
                result.put("fail","绑定失败,请检查密码是是否正确");
            }
        }else{
            result.put("fail","绑定失败,请检查信息是否有误");
        }
        return result;
    }

    @Override
    public boolean checkBinding(BindingBankDTO bindingBankDTO) {

        if(null != virtualBankMapper.selectVirtualBankByBankNumberAndUserId(bindingBankDTO)){
            return false;
        }else{
            return true;
        }
    }

    @Override
    public Integer cancelBinding(CancelBindingDTO cancelBindingDTO) {
        return virtualBankMapper.delVirtualBankRelation(cancelBindingDTO);
    }

    @Override
    public List<BindingRelationsVO> existBank(Integer userId) {
        return virtualBankMapper.selectRelations(userId);
    }
}
